package thulac;

import java.io.*;

import manage.Filter;
import manage.NegWord;
import manage.Postprocesser;
import manage.Preprocesser;
import manage.Punctuation;
import manage.TimeWord;
import manage.VerbWord;

import base.POCGraph;
import base.TaggedSentence;

import character.CBTaggingDecoder;

public class thulac {

	/**
	 * @param args
	 * @throws IOException 
	 */
	
	public static void main(String[] args) throws IOException {
		String user_specified_dict_name=null;
	    String model_path_char = null ;

	    Character separator = '_';

	    boolean useT2S = false;
	    boolean seg_only = false;
	    boolean useFilter = false;
	    boolean use_second = false;
	    
	    int c = 1;
	    while(c < args.length){
	        String arg = args[c];
	        if(arg == "-t2s"){
	            useT2S = true;
	        }else if(arg == "-user"){
	            user_specified_dict_name = args[++c];
	        }else if(arg == "-deli"){
	            separator = args[++c].charAt(0);
	        }else if(arg == "-seg_only"){
	            seg_only = true;
	        }else if(arg == "-use_second"){
	            use_second = true;
	        }else if(arg == "-filter"){
	            useFilter = true;
	        }else if(arg == "-model_dir"){
	            model_path_char = args[++c];
	        }else{
	            //showhelp();
	        	return;
	        }
	        c ++;
	    }

	    
	    String prefix;
	    if(model_path_char != null){
	        prefix = model_path_char;
	        if(prefix.charAt(prefix.length()-1) != '/'){
	            prefix += "/";
	        }
	    }else{
	        prefix = "models/";
	    }

	    String oiraw;
	    String raw =new String();
	    POCGraph poc_cands = new POCGraph();
	    TaggedSentence tagged = new TaggedSentence();
	    
	    CBTaggingDecoder tagging_decoder=new CBTaggingDecoder();
	    tagging_decoder.threshold = 10000;
	   
	    tagging_decoder.init((prefix+"model_c_model.bin"),(prefix+"model_c_dat.bin"),(prefix+"model_c_label.txt"));
	    tagging_decoder.setLabelTrans();
	    
	    
	    Preprocesser preprocesser = new Preprocesser();
	    Postprocesser nsDict = new Postprocesser((prefix+"ns.dat"), "ns", false);
	    Postprocesser idiomDict = new Postprocesser((prefix+"idiom.dat"), "i", false);
	    Postprocesser userDict = null;
	    if(user_specified_dict_name!=null){
	        userDict = new Postprocesser(user_specified_dict_name, "uw", true);
	    }
	    Punctuation punctuation = new Punctuation((prefix+"singlepun.dat"));
	    TimeWord timeword = new TimeWord();
	    NegWord negword = new NegWord((prefix+"neg.dat"));
	    VerbWord verbword = new VerbWord((prefix+"vM.dat"), (prefix+"vD.dat"));
	    Filter filter = null;
	    if(useFilter){
	        filter = new Filter((prefix+"xu.dat"), (prefix+"time.dat"));
	    }
	  // Postprocesser nz_dict = new Postprocesser((prefix+"nz.txt"), "nz", true);
	    // Postprocesser ni_dict = new Postprocesser((prefix+"ni.txt"), "ni", true);
	    // Postprocesser noun_dict = new Postprocesser((prefix+"noun.txt"), "n", true);
	    // Postprocesser adj_dict = new Postprocesser((prefix+"adj.txt"), "a", true);
	    // Postprocesser verb_dict = new Postprocesser((prefix+"verb.txt"), "v", true);
	    // Postprocesser vm_dict = new Postprocesser((prefix+"vm.txt"), "vm", true);
	    // Postprocesser y_dict = new Postprocesser((prefix+"y.txt"), "y", true);
	    
	    
	    
	    File file = new File("cs.txt");
		BufferedReader reader = null;
        try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        FileOutputStream out=new FileOutputStream("cs.out");
        
        //tagging_decoder.cs();
	    while(true)
	    {
	    	oiraw=getRaw(reader);
	    	if(oiraw==null) break;
	    	//raw=oiraw;
	    	raw=preprocesser.clean(oiraw,poc_cands);
	    	//System.out.println(raw);
	    	//System.out.println(poc_cands.size());
	    	if(raw.length()>0)
	    	{
	    		/*poc_cands.clear();
	    		for(int i=0;i<(int)raw.length();i++)
	    		{
	    			if(i==0)
	    			{
	    				poc_cands.add(9);
	    				continue;
	    			}
	    			if(i+1==raw.length())
	    			{
	    				poc_cands.add(12);
	    				continue;
	    			}
	    			poc_cands.add(15);
	    		}*/
	    		tagging_decoder.segment(raw, poc_cands, tagged);
	    		
	    		nsDict.adjust(tagged);
	    		idiomDict.adjust(tagged);
	    		if(userDict!=null){
                    userDict.adjust(tagged);
                }
	    		punctuation.adjust(tagged);
	    		timeword.adjustDouble(tagged);
	    		negword.adjust(tagged);
	    		verbword.adjust(tagged);
	    		if(useFilter){
                    filter.adjust(tagged);
                }
	    		
	    		
	    		// nz_dict.adjust(tagged);
                // ni_dict.adjust(tagged);
                // noun_dict.adjust(tagged);
                // adj_dict.adjust(tagged);
                // verb_dict.adjust(tagged);
                // vm_dict.adjustSame(tagged);
                // y_dict.adjustSame(tagged);
	    		
	    		for(int i=0;i<tagged.size();i++) tagged.get(i).print(out);
	    		out.write('\n');
	    	}
	    }
	    
	    
	}
	public static String getRaw(BufferedReader reader)
	{
		String ans=null;
		try {
			while((ans = reader.readLine()) != null)
			{
				break;	
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(ans);
		return ans;
	}
}
